package hh.util.bean.copy;

import hh.util.bean.ClassInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huanghuai
 * @date 2020/10/26
 */
public abstract class AbstractCopier implements BeanCopier {

    private static final long serialVersionUID = -5123570321338282255L;
    private static Map<Class, ClassInfo> typeToInfo = new HashMap<>(1024);

    @Override
    public <Source, Dest> Dest copy(Source sourceObj, Class<Dest> destType) {
        try {
            return doCopy(sourceObj, destType);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }



    protected abstract <Dest, Source> Dest doCopy(Source sourceObj, Class<Dest> destType) throws Exception;

    public ClassInfo getClassInfo(Class clazz) {
        ClassInfo info = typeToInfo.get(clazz);
        if (info == null) {
            typeToInfo.put(clazz, info = new ClassInfo(clazz));
            doInitClassInfo(info);
        }
        return info;
    }
    public ClassInfo putClassInfo(Class clazz,ClassInfo classInfo) {
        return typeToInfo.putIfAbsent(clazz,classInfo);
    }

    protected void doInitClassInfo(ClassInfo classInfo) {
    }


    @Override
    public <Source, Dest> Dest copy(Source sourceObj, Dest dest) {
        return null;
    }
}
